<template>
  <div>
    <el-card>
      <el-table style="width: 100%" border :data="skuArr">
        <el-table-column
          label="序号"
          width="width"
          type="index"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="skuName"
          label="名称"
          width="width"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="skuDesc"
          label="描述"
          width="width"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="图片" width="width" align="center">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" width="100px" height="100px" />
          </template>
        </el-table-column>
        <el-table-column
          prop="weight"
          label="重量"
          width="width"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="price"
          label="价格"
          width="width"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" width="width" fixed="right">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              :icon="row.isSale == 1 ? 'bottom' : 'top'"
              @click="updateSale(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="edit"
              @click="updateSku"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="InfoFilled"
              @click="findSku(row)"
            ></el-button>
            <el-popconfirm
              :title="`你确定要删除${row.skuName}?`"
              width="200px"
              @click="removeSku(row.id)"
            >
              <template #reference>
                <el-button
                  type="primary"
                  size="small"
                  icon="delete"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageNo"
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="prev, pager, next,jumper,->,sizes,total"
        :total="total"
        @current-change="getHasSku"
        @size-change="getHasSku(1)"
      ></el-pagination>
      <!-- 抽屉组件：展示商品的详情 -->
      <el-drawer title="商品详情" v-model="drawer">
        <template #default>
          <el-row>
            <el-col :span="6">名称</el-col>
            <el-col :span="18">{{ skuInfo.skuName }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6">描述</el-col>
            <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6">价格</el-col>
            <el-col :span="6">{{ skuInfo.price }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                type="success"
                v-for="(item, index) in skuInfo.skuAttrValueList"
                :key="item.id"
              >
                {{ item.valueName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                type="success"
                v-for="(item, index) in skuInfo.skuSaleAttrValueList"
                :key="item.id"
              >
                {{ item.saleAttrValueName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <el-carousel :interval="4000" type="card" height="200px">
                <el-carousel-item
                  v-for="item in skuInfo.skuImageList"
                  :key="item.id"
                >
                  <img :src="item.imgUrl" alt="" width="100%" height="100%" />
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </template>
      </el-drawer>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSaleSku,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku'
import type {
  SkuResponseData,
  SkuData,
  SkuInfoData,
} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'

let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
// 存储SKU数据
let skuArr = ref<SkuData[]>([])
// 控制抽屉显示与隐藏
let drawer = ref<boolean>(false)
let skuInfo = ref<SkuData>({})
// 组件挂载完毕
onMounted(() => {
  getHasSku()
})
// 获取SKU数据
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code == 200) {
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}
// 商品的上架与下架
const updateSale = async (row: SkuData) => {
  // 1 上架状态，0 下架状态
  if (row.isSale == 1) {
    await reqCancelSaleSku(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
    row.isSale = 0
  } else {
    reqSaleSku(row.id as number)
    ElMessage({
      type: 'success',
      message: '上架成功',
    })
    row.isSale = 1
  }
}
// 更新已有的SKU
const updateSku = () => {
  ElMessage({ type: 'success', message: '工作人员正在更新中....' })
}
// 查看商品详情的回调
const findSku = async (row: SkuData) => {
  drawer.value = true
  // 获取已有商品的详情数据
  let result: SkuInfoData = await reqSkuInfo(row.id as number)
  // 存储已有的SKU
  skuInfo.value = result.data
}
// 删除某个已有的商品
const removeSku = async (id: number) => {
  let result = await reqRemoveSku(id)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({ type: 'error', message: '删除失败' })
  }
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
